/*
 * Archive class based on AWS SDK code samples
 * Supports upload, download, delete operations
 * 
 * Distributed under Apache 2.0 license
 * Author: David Mail, email: david.mail37(at)gmail(dot)com
 * October 7 2012
 */

package com.pixamba.aws.glacier;

import com.amazonaws.services.glacier.model.DeleteArchiveRequest;
import com.amazonaws.services.glacier.transfer.ArchiveTransferManager;
import com.amazonaws.services.glacier.transfer.UploadResult;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 *
 * @author david
 */
public class AwsGlacierArchive extends AwsGlacier{
    protected ArchiveTransferManager atm = null;
    
    public AwsGlacierArchive( String configFile) throws IOException {
        super( configFile);
        this.atm = new ArchiveTransferManager( this.getClient(), this.getCredentials());        
    }
    
    public ArchiveTransferManager getArchiveTransferManager(){
        return this.atm;
    }
    
    /*
     * uploads a file to a vault and gives it the specified archive name
     * @return new archive ID
     */
    public String upload( String vaultName, String newArchiveName, String file) throws FileNotFoundException {
        return getArchiveTransferManager().upload( vaultName, newArchiveName, new File( file)).getArchiveId();
    }
    
    /*
     * Downloads an archve, sppecified by its ID and its vault name to a given file
     * A new file is created
     * Each download takes AT LEAST 4 hours (get prepared to 24+ hours)! This way AWS Glacier works - by design!
     */
    public void download( String vaultName, String archiveId, String file){
        getArchiveTransferManager().download(vaultName, archiveId, new File( file));
    }
    
    /*
     * Deletes an archive specified by vault name and archive ID
     */
    public void delete( String vaultName, String archiveId){
        this.getClient().deleteArchive( new DeleteArchiveRequest().withVaultName( vaultName).withArchiveId(archiveId));
    }
   
}